/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.ExcepcionRol;
import ManejoExc.PersonalException;
import Negocio.Empresa;
import Negocio.TipoRol;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author robert
 */
public class FrmTipoRol extends javax.swing.JDialog {

    private Empresa miEmpresa;
    private TipoRol unRol;
    DefaultListModel modelTipoRoles = new DefaultListModel();
    
    
    /**
     * Creates new form jDlgTipoRol
     */
    public FrmTipoRol(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public FrmTipoRol(java.awt.Frame parent, boolean modal, Empresa miEmpresa) {
        //super(parent, modal);
        this.miEmpresa = miEmpresa;
        initComponents();
        this.LstTiposRoles.setModel(modelTipoRoles);
        this.cargarTiposRoles();
        this.setTitle("Tipos de Roles");
       
    }
    
    public void cargarTiposRoles(){
        this.modelTipoRoles.clear();
            try{
            if(this.miEmpresa.getTipoDeRolesActivos().size()> 0){
                this.miEmpresa.ordenarTipoRol(this.miEmpresa.getTipoDeRoles(),1);
                Iterator it = this.miEmpresa.getTipoDeRolesActivos().iterator();
                //Iterator it = this.miEmpresa.getTipoDeRoles().iterator();
                while(it.hasNext() ){
                    this.modelTipoRoles.addElement(it.next());
                }
            }else{
                this.modelTipoRoles.addElement("No se encontraron tipos de Roles");
            }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        LstTiposRoles = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jTxtNombre = new javax.swing.JTextField();
        jBtnAgregar = new javax.swing.JButton();
        BtnAceptar = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();
        BtnNuevo = new javax.swing.JButton();
        BtnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        LstTiposRoles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstTiposRoles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstTiposRolesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LstTiposRoles);

        jLabel1.setText("Nombre");

        jTxtNombre.setEnabled(false);

        jBtnAgregar.setText("Guardar");
        jBtnAgregar.setEnabled(false);
        jBtnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgregarActionPerformed(evt);
            }
        });

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.setEnabled(false);
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnCancel.setText("Cancelar");
        BtnCancel.setEnabled(false);
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(BtnAceptar)
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(20, 20, 20)
                                .addComponent(jTxtNombre))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BtnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnAgregar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnBorrar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnCancel)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBtnAgregar)
                            .addComponent(BtnBorrar)
                            .addComponent(BtnNuevo)
                            .addComponent(BtnCancel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnAceptar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgregarActionPerformed
        try{
            String nombre = this.jTxtNombre.getText().trim();
            
            if(this.unRol == null){
                this.miEmpresa.nuevoTipoRol(nombre);
            }else{
                this.miEmpresa.actualizarTipoRol(this.unRol,this.jTxtNombre.getText());                 
                this.unRol = null; 
            }
             JOptionPane.showMessageDialog(this, "La operacion se realizo satisfactoriamente");            
        }
        catch (ExcepcionRol ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
            this.jTxtNombre.selectAll();
        }
        finally{
            this.BtnBorrar.setEnabled(false);
            this.jBtnAgregar.setEnabled(false);
            this.BtnNuevo.setEnabled(true);
            this.jTxtNombre.setEnabled(false);
            this.cargarTiposRoles();
            this.jTxtNombre.setText("");
            this.BtnCancel.setEnabled(false);
            
            


        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jBtnAgregarActionPerformed

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void LstTiposRolesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstTiposRolesMouseClicked
        // TODO add your handling code here:

        
        if(this.modelTipoRoles.size() > 0){ //Si hay algo cargado
            this.unRol = (TipoRol) this.modelTipoRoles.getElementAt(this.LstTiposRoles.getSelectedIndex());
            jTxtNombre.setText(this.unRol.getNombre());
            this.BtnBorrar.setEnabled(true);
            this.jBtnAgregar.setEnabled(true);
            this.BtnNuevo.setEnabled(false);
            this.jTxtNombre.setEnabled(true);
            this.BtnCancel.setEnabled(true);
            
        }
        
    }//GEN-LAST:event_LstTiposRolesMouseClicked

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.BtnBorrar.setEnabled(false);
        this.jBtnAgregar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        this.jTxtNombre.setEnabled(true);
        this.BtnCancel.setEnabled(true);
        this.unRol = null;
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        int mc,bc,ch=1;
        mc = JOptionPane.QUESTION_MESSAGE;
        bc = JOptionPane.YES_NO_CANCEL_OPTION;
        ch = JOptionPane.showConfirmDialog (this, "Desea dar de baja el tipo de rol seleccionado?", "Confirmacion", bc, mc); 
        if(this.unRol != null && ch == 0){

                this.unRol.cambiarEstado(false);
                this.cargarTiposRoles();
                this.jTxtNombre.setText("");
                this.jTxtNombre.setEnabled(true);
                this.BtnNuevo.setEnabled(true);
                this.BtnCancel.setEnabled(false);
                this.BtnBorrar.setEnabled(false);
                this.jBtnAgregar.setEnabled(false);
                
            
        }
    }//GEN-LAST:event_BtnBorrarActionPerformed

    private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
        // TODO add your handling code here:
        this.unRol = null;
        this.cargarTiposRoles();
        this.BtnNuevo.setEnabled(true);
        this.BtnCancel.setEnabled(false);
        this.BtnBorrar.setEnabled(false);
        this.BtnAceptar.setEnabled(false);
        this.jTxtNombre.setText("");
        this.jTxtNombre.setEnabled(false);
        
    }//GEN-LAST:event_BtnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmTipoRol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmTipoRol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmTipoRol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmTipoRol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmTipoRol dialog = new FrmTipoRol(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JList LstTiposRoles;
    private javax.swing.JButton jBtnAgregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTxtNombre;
    // End of variables declaration//GEN-END:variables
}
